import json

import numpy as np
import pymysql
import scrapy
from scrapy.utils.project import get_project_settings

from ..items import NewbilispiderScrapyItem

table = 'fZodR9XQDSUm21yCkr6zBqiveYah8bt4xsWpHnJE7jL5VG3guMTKNPAwcF'
tr = {}
for i in range(58):
    tr[table[i]] = i
s = [11, 10, 3, 8, 4, 6]
xor = 177451812
add = 8728348608


def dec(x):
    r = 0
    for i in range(6):
        r += tr[x[s[i]]] * 58 ** i
    return (r - add) ^ xor


def enc(x):
    x = (x ^ xor) + add
    r = list('BV1  4 1 7  ')
    for i in range(6):
        r[s[i]] = table[x // 58 ** i % 58]
    return ''.join(r)


def getbvlist():
    settings = get_project_settings()
    host = settings['DB_HOST']
    port = settings['DB_PORT']
    user = settings['DB_USER']
    password = settings['DB_PASSWORD']
    name = settings['DB_NAME']
    charset = settings['DB_CHARSET']
    connect = pymysql.connect(
        host=host,
        port=port,
        user=user,
        password=password,
        db=name,
        charset=charset,
    )
    # 通过cursor执行增删查改
    cursor = connect.cursor()
    cursor.execute("select bvid from hot_list")
    bvlist = np.array(cursor.fetchall())
    connect.commit()
    # cursor.close()
    # connect.close()
    return bvlist


class PinglunSpider(scrapy.Spider):
    name = 'pinglun'
    allowed_domains = ['https://api.bilibili.com']
    s = 1
    count = 0
    num = 0
    # demo = [['BV1pL411N79t'], ['BV1ZP4y1G7nf']]
    # bv = str(demo[num][0])
    bv = str(getbvlist()[num][0])
    oid = dec(bv)
    # oid = 211721245
    start_urls = ['https://api.bilibili.com/x/v2/reply?type=1&mode=3&oid=' + str(oid) + '&pn=1']

    def parse(self, response):
        items = NewbilispiderScrapyItem()
        alldata = json.loads(response.text)
        data = alldata['data']
        comment = data['replies']
        print(str(self.count))
        for i in comment:
            items['bv'] = enc(i['oid'])
            items['userlevel'] = i['member']['level_info']['current_level']
            items['username'] = i['member']['uname']
            items['senddate'] = i['ctime']
            items['sex'] = i['member']['sex']
            items['content'] = i['content']['message']
            items['likes'] = i['like']
            self.count += 1

            yield items

        if len(comment):
            self.s += 1
            url = 'https://api.bilibili.com/x/v2/reply?type=1&mode=3&oid={}&pn={}'.format(str(self.oid), str(self.s))

            yield scrapy.Request(url=url, callback=self.parse, dont_filter=True)
        else:
            self.num += 1
            if self.num < 200:
                self.s = 1
                self.oid = dec(str(getbvlist()[self.num][0]))
                # self.oid = dec(str(self.demo[self.num][0]))

                url = 'https://api.bilibili.com/x/v2/reply?type=1&mode=3&oid={}&pn={}'.format(str(self.oid), str(self.s))

                yield scrapy.Request(url=url, callback=self.parse, dont_filter=True)

